package vue;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import modele.CKModel;

import controleur.ControleAddConcept;
import controleur.ControleRdbPartition;
import controleur.ControleReturn;

public class PanelPartition extends JPanel {
	
	static final long serialVersionUID = 12345;
	
	CKModel<String, String> _modele;
	VueEditeurCK _vue;
	
	private JLabel lblChild;
	private JTextField txtChild;
	
	private JLabel lblFather;
	private JComboBox cmbFather;
	
	private JLabel lblChooseK;
	private ButtonGroup choix;

	private JRadioButton rbOdlK;
	private JComboBox cmbOldK;

	private JRadioButton rbNewK;
	private JScrollPane scp;
	private JTextArea txtNewK;
	
	private JButton btnAddC;
	private JButton btnR;

	public PanelPartition(CKModel<String, String> modele, VueEditeurCK vue){
		
		_modele = modele;
		_vue = vue;
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		this.setPreferredSize(new Dimension(_vue.getFrameWidth(), _vue.getFrameHeight()/3));
		gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.ipadx = 0;
        gbc.ipady = 0;
        Insets i = new Insets(5, 5, 5, 5);
		gbc.insets = i;
        gbc.anchor = GridBagConstraints.CENTER;
		
		
				
		lblFather = new JLabel("Concept à partitioner : ");
		lblFather.setPreferredSize(new Dimension(200,30));
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
		this.add(lblFather, gbc);
		
		cmbFather = new JComboBox();
		cmbFather.setPreferredSize(new Dimension(200,30));
		this.LoadConcept();
		gbc.gridx = 1;
		gbc.gridy = 0;
		this.add(cmbFather, gbc);
		
		lblChooseK = new JLabel("Quelle connaissance vous permet de partionner :");
		lblChooseK.setPreferredSize(new Dimension(400,30));
		gbc.gridx = 3;
		gbc.gridy = 0;
		gbc.gridwidth = GridBagConstraints.RELATIVE;
		this.add(lblChooseK, gbc);
		
		
		lblChild = new JLabel("Entrez la partition :");
		lblChild.setPreferredSize(new Dimension(200,30));
		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 1;
		this.add(lblChild, gbc);
		
		txtChild = new JTextField();
		txtChild.setPreferredSize(new Dimension(200,30));
		gbc.gridx = 1;
		gbc.gridy = 2;
		this.add(txtChild, gbc);
		
		
		
		
		choix = new ButtonGroup();
		
		
		rbOdlK =  new JRadioButton("Les connaissances existantes :", true);
		rbOdlK.addActionListener(new ControleRdbPartition(this));
		choix.add(rbOdlK);
		gbc.gridx = 3;
		gbc.gridy = 2;
		this.add(rbOdlK, gbc);
		
		cmbOldK = new JComboBox();
		cmbOldK.setPreferredSize(new Dimension(200,30));
		this.LoadKnowledge();
		gbc.gridx = 4;
		gbc.gridy = 2;
		this.add(cmbOldK, gbc);
		
		
		rbNewK = new JRadioButton("Créer une nouvelle connaissance : ", false);
		rbNewK.addActionListener(new ControleRdbPartition(this));
		choix.add(rbNewK);
		gbc.gridx = 3;
		gbc.gridy = 3;
		this.add(rbNewK, gbc);
		
		txtNewK = new JTextArea();
		txtNewK.setLineWrap(true);
		scp = new JScrollPane(txtNewK, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
		        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scp.setPreferredSize(new Dimension(300,300));
		gbc.gridx = 4;
		gbc.gridy = 3;
		gbc.weighty = 2;
		gbc.weightx = 2;
		this.add(scp, gbc);
		
		
		btnAddC = new JButton("Partitioner");
		btnAddC.setPreferredSize(new Dimension(200,30));
		btnAddC.addActionListener(new ControleAddConcept(_modele, _vue));
		gbc.gridx = 4;
		gbc.gridy = 5;
		gbc.gridwidth = GridBagConstraints.RELATIVE;
        gbc.gridheight = 1;
        gbc.weightx = 0.0;
        gbc.weighty = 0.0;
		this.add(btnAddC, gbc);
		
		btnR = new JButton("Retour");
		btnR.setPreferredSize(new Dimension(200,30));
		btnR.addActionListener(new ControleReturn(_modele, _vue));
		gbc.gridx = 4;
		gbc.gridy = 6;
		this.add(btnR, gbc);
		
		this.chooseOld();
		
		
	}

	/**
	 * @return the txtChild
	 */
	public String getTxtChild() {
		return this.txtChild.getText();
	}
	
	public String getTxtNewK(){
		return this.txtNewK.getText();
	}
	
	public JRadioButton getRdbOld(){
		return this.rbOdlK;
	}
	
	public JRadioButton getRdbNew(){
		return this.rbNewK;
	}
	
	public JComboBox getCmbFather(){
		return this.cmbFather;
	}
	
	public JComboBox getCmbOldK(){
		return this.cmbOldK;
	}
	
	@SuppressWarnings("deprecation")
	public void chooseOld(){
		this.txtNewK.enable(false);
		this.cmbOldK.enable(true);
	}
	
	@SuppressWarnings("deprecation")
	public void chooseNew(){
		this.txtNewK.enable(true);
		this.cmbOldK.enable(false);
	}
	
	public void LoadConcept(){
		cmbFather.removeAllItems();
		for (int i=0; i<_modele.getCSize(); i++){
			this.cmbFather.addItem(_modele.findConceptFromId(i));
		}
	}
	
	public void LoadKnowledge(){
		cmbOldK.removeAllItems();
		for(int i=0; i<_modele.getKSpace().size(); i++){
			this.cmbOldK.addItem(_modele.getKSpace().elementAt(i));
		}
	}
	

	/**
	 * @return the btnR
	 */
	public JButton getBtnR() {
		return btnR;
	}
	
	
}
